home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / AppsToGo / Kibitz / IdleTasks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  5.7 KB  |  217 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        idletasks.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __UTILITIES__
  21. #include <Utilities.h>
  22. #endif
  23.  
  24.  
  25.  
  26. /*****************************************************************************/
  27. /*****************************************************************************/
  28.  
  29. #ifdef applec
  30. #pragma segment IdleTasks
  31. #endif
  32.  
  33. /*****************************************************************************/
  34. /*****************************************************************************/
  35.  
  36.  
  37.  
  38. void    DoIdleTasks(Boolean allowComputerMoves)
  39. {
  40.     WindowPtr        window, compMoveWindow;
  41.     unsigned long    compMoveTick;
  42.     FileRecHndl        game, compMoveGame;
  43.     FileRecPtr        frPtr;
  44.     short            twoPlayer, moveColor, myColor, update, syncClocks, i;
  45.     Boolean            compMovesWhite, compMovesBlack, sendSyncGame;
  46.     OSErr            err;
  47.  
  48.     static unsigned long    clockSyncTick;
  49.     static Boolean            startingUp = true;
  50.     static long                waitSome;
  51.  
  52.     if (startingUp) {
  53.         if (FrontWindow()) {
  54.             startingUp = false;
  55.             return;
  56.         }
  57.         if (!waitSome) waitSome = TickCount() + 30;
  58.         else {
  59.             if (waitSome < TickCount()) {
  60.                 startingUp = false;
  61.                 err = AppNewDocument(&game, ksOrigName);
  62.                 if (!err) {
  63.                     (*game)->doc.compMovesBlack = true;
  64.                     err = AppNewWindow(game, nil, (WindowPtr)-1);
  65.                     if (err) {
  66.                         AppDisposeDocument(game);
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.         return;
  72.     }
  73.  
  74.     DynamicBalloonHelp();
  75.  
  76.     compMoveTick = -1;
  77.  
  78.     syncClocks = (clockSyncTick + 1800 < TickCount());
  79.     if (syncClocks)
  80.         clockSyncTick = TickCount();
  81.             /* Syncronize clocks every 30 seconds. */
  82.  
  83.  
  84.     for (window = FrontWindow();
  85.          window;
  86.          window = (WindowPtr)(((WindowPeek)window)->nextWindow)
  87.     ) {
  88.         if (IsAppWindow(window)) {
  89.  
  90.             DoSetCursor(nil);
  91.  
  92.             game = (FileRecHndl)GetWRefCon(window);
  93.             twoPlayer = (*game)->doc.twoPlayer;
  94.  
  95.             update = UpdateTime(game, true);
  96.             if (update) DrawTime(game);
  97.  
  98.             if (update == 2) {
  99.                 if (twoPlayer) SendGame(game, kIsMove, nil);
  100.                     /* Send it as a move, since we want the alert to
  101.                     ** show up for the opponent. */
  102.                 AlertIfGameOver(game);
  103.                 return;
  104.             }
  105.  
  106.             if ((*game)->doc.resync >= kResync) {
  107.                 sendSyncGame = true;
  108.                     /* We may need to sync up.  Assume we will. */
  109.  
  110.                 if ((*game)->doc.configColorChange) {
  111.                     if ((*game)->doc.myColor != (*game)->doc.configColor) {
  112.                         (*game)->doc.myColor = (*game)->doc.configColor;
  113.                         (*game)->doc.invertBoard ^= 1;
  114.                         SetPort(window);
  115.                         ImageDocument(game, true);
  116.                     }
  117.                     (*game)->doc.configColorChange = false;
  118.                     AdjustGameSlider(game);
  119.                     sendSyncGame = false;
  120.                 }
  121.  
  122.                 if ((*game)->doc.configTimeChange) {
  123.                     for (i = 0; i < 2; ++i)
  124.                         (*game)->doc.timeLeft[i] =
  125.                             (*game)->doc.displayTime[i] =
  126.                                 (*game)->doc.configTime[i];
  127.                     (*game)->doc.configTimeChange = false;
  128.                     UpdateTime(game, false);
  129.                     DrawTime(game);
  130.                     sendSyncGame = false;
  131.                 }
  132.  
  133.                 if (sendSyncGame) {
  134.                     if ((*game)->doc.gotUpdateTick + 120 < TickCount()) {
  135.                         /* Wait for 2 secs since last game update before syncing.
  136.                         ** This is so that we don't send a sync while the opponent
  137.                         ** is still clicking on the arrow.  Without this delay, the
  138.                         ** scrollbar of the opponent will jump around after he is
  139.                         ** done scrolling.  It would eventually end up correct,
  140.                         ** but it looks bad.  2 secs is enough time (generally) for
  141.                         ** the opponent to receive the sync from the last click, so
  142.                         ** the scroll won't jump around.  Also, 2 secs is probably
  143.                         ** more time than the user would take between clicks on
  144.                         ** the arrow. */
  145.  
  146.                         if ((*game)->doc.creator)        /* Only the creator can sync. */
  147.                             if (twoPlayer) SendGame(game, kHandResync, nil);
  148.                                 /* Make sure the boards are in sync. */
  149.  
  150.                         (*game)->doc.resync = kIsMove;
  151.                             /* Back to life as usual. */
  152.  
  153.                         DoSetCursor(nil);
  154.                             /* Re-calc the cursor. */
  155.                     }
  156.                 }
  157.             }
  158.  
  159.             frPtr          = *game;
  160.             compMovesWhite = frPtr->doc.compMovesWhite;
  161.             compMovesBlack = frPtr->doc.compMovesBlack;
  162.             myColor        = frPtr->doc.myColor;
  163.             moveColor      = WhosMove(game);
  164.  
  165.             if (frPtr->doc.arrangeBoard)
  166.                 compMovesWhite = compMovesBlack = 0;
  167.  
  168.             if (twoPlayer) {
  169.                 if (myColor == moveColor) {
  170.                     if (syncClocks)
  171.                         SendMssg(game, kTimeMssg);
  172.                             /* Sync up the clocks every 30 seconds.  Only do
  173.                             ** this if it is our move, since the player who
  174.                             ** owns the move also owns the clock. */
  175.                 }
  176.                 else
  177.                     compMovesWhite = compMovesBlack = false;
  178.             }
  179.             if (
  180.                 ((compMovesWhite) && (moveColor == WHITE)) ||
  181.                 ((compMovesBlack) && (moveColor == BLACK))
  182.             ) {
  183.                 if (compMoveTick > frPtr->doc.compMoveTick) {
  184.                     if (GameStatus(game) == kGameContinues) {
  185.                         compMoveTick   = frPtr->doc.compMoveTick;
  186.                         compMoveWindow = window;
  187.                         compMoveGame   = game;
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.     }
  193.  
  194.     if (allowComputerMoves) {
  195.         if (compMoveTick != -1) {
  196.             if ((*compMoveGame)->doc.resync == kIsMove) {
  197.                 (*compMoveGame)->doc.compMoveTick = TickCount();
  198.                 if (ComputerMove(compMoveGame)) {
  199.                     SetPort(compMoveWindow);
  200.                     ImageDocument(compMoveGame, true);
  201.                     AdjustGameSlider(compMoveGame);
  202.                     twoPlayer = (*compMoveGame)->doc.twoPlayer;
  203.                     DrawButtonTitle(compMoveGame, twoPlayer);
  204.                     UpdateGameStatus(compMoveGame);
  205.                     if (twoPlayer) SendGame(compMoveGame, kIsMove, nil);
  206.                     SayTheMove(compMoveGame);
  207.                     AlertIfGameOver(compMoveGame);
  208.                     DoSetCursor(nil);
  209.                 }
  210.             }
  211.         }
  212.     }
  213. }
  214.  
  215.  
  216.  
  217.